const router = require('express').Router()
const validateToken = require('../../../../common/validateToken')
const status_code = require('../../../../common/status_code')
const db = require('../../../../db/async-db')
const { to } = require('await-to-js')
const Bee = require('../../../../common/bee/bee')

router.post('/addMenu', async (req, resp) => {
  // 错误处理
  const handleError = (errCode, error) => {
    resp.json({
      code: errCode,
      msg: status_code[errCode],
    })
    console.error(error)
  }

  // 从请求头里取出 token
  const rawToken = req.headers.authorization
  // 验证token
  const [validateTokenError, validateTokenSuccess] = await to(
    validateToken(rawToken)
  )
  // 验证不通过处理
  if (validateTokenError || !validateTokenSuccess)
    return handleError(66013, validateTokenError)

  const {
    parent_id,
    name,
    node_type,
    icon_url,
    selected_icon_url,
    link_url,
    menu_status,
    sort,
    router_name,
  } = req.body

  if (
    !Bee.StringUtils.isNotBlank(name) ||
    !Bee.StringUtils.isNumber(parent_id) ||
    !Bee.StringUtils.isNumber(node_type) ||
    !Bee.StringUtils.isNumber(sort) ||
    !Bee.StringUtils.isNumber(menu_status)
  )
    return handleError(10003, '参数错误')

  if (node_type === 2 && !Bee.StringUtils.isNotBlank(link_url))
    return handleError(10003, '参数错误')

  // 查询菜单
  const [findMenuError, MenuList] = await to(
    db.query('SELECT * FROM menu WHERE id=?', [parent_id])
  )
  // 判断是否有错误 或 是否有该菜单
  if (findMenuError || !MenuList || (parent_id !== 0 && MenuList.length !== 1))
    return handleError(-1, findMenuError)
  const Menu = MenuList[0]

  if (node_type === 1) {
    // 添加菜单-文件夹
    const [insertMenuError, insertMenuSuccess] = await to(
      db.query(
        'INSERT INTO menu (name, parent_id, node_type, level, path, menu_status, sort, icon_url, selected_icon_url, router_name) VALUES (?, ?, 1, ?, ?, ?, ?, ?, ?, ?)',
        [
          name,
          parent_id,
          parent_id !== 0 ? (Menu.level += 1) : 1,
          parent_id !== 0
            ? Menu.path + Menu.path !== ''
              ? Menu.path + ',' + parent_id
              : '' + parent_id
            : '',
          menu_status,
          sort,
          Bee.StringUtils.isNotBlank(icon_url) ? icon_url : '',
          Bee.StringUtils.isNotBlank(selected_icon_url)
            ? selected_icon_url
            : '',
          router_name ? router_name : '',
        ]
      )
    )
    if (insertMenuError || !insertMenuSuccess)
      return handleError(-1, findMenuError)

    return resp.json({
      code: 0,
      msg: status_code[0],
    })
  } else if (node_type === 2) {
    // 添加菜单-页面
    console.log(parent_id !== 0 && Menu.parent_id !== 0)
    console.log(parent_id, Menu.parent_id)
    const [insertMenuError, insertMenuSuccess] = await to(
      db.query(
        'INSERT INTO menu (name, parent_id, node_type, level, path, menu_status, sort, link_url, icon_url, selected_icon_url) VALUES (?, ?, 2, ?, ?, ?, ?, ?, ?, ?)',
        [
          name,
          parent_id,
          parent_id !== 0 ? (Menu.level += 1) : 1,
          parent_id !== 0
            ? Menu.path + Menu.path !== ''
              ? Menu.path + ',' + parent_id
              : '' + parent_id
            : '',
          menu_status,
          sort,
          link_url,
          Bee.StringUtils.isNotBlank(icon_url) ? icon_url : '',
          Bee.StringUtils.isNotBlank(selected_icon_url)
            ? selected_icon_url
            : '',
        ]
      )
    )
    if (insertMenuError || !insertMenuSuccess)
      return handleError(-1, findMenuError)

    return resp.json({
      code: 0,
      msg: status_code[0],
    })
  }
})

module.exports = router
